<?php

namespace Home\Logic;

/**
 * 用户逻辑
 */
class UserLogic extends BaseLogic{

    /**
     * 生成推荐码
     * @param $id
     * @return string
     */
    public static function buildEncodeReferralCode($id)
    {
        $hashids = new \Hashids\Hashids('education', 6, 'abcdefghijklmnopqrstuvwxyz');
        $hashID = $hashids->encode($id);
        return $hashID;
    }

    /**
     * 解码推荐码，获得推广人id
     * @param $referral_code
     * @return array
     */
    public static function getDecodeReferralCode($referral_code)
    {
        $hashids = new \Hashids\Hashids('education', 6, 'abcdefghijklmnopqrstuvwxyz');
        $decodeResult = $hashids->decode($referral_code);
        return $decodeResult;
    }

    /**
     * 增加上级的推广数量
     * @param int $uid 用户id
     */
    public static function addParentsTuiguangNum($uid){
        $ptyh = M('ptyh')->where(['uid' => $uid])->find();
        $pathArr = explode('/',$ptyh['path']);
        array_pop($pathArr);
        if(!empty($pathArr)){
            foreach($pathArr as $k => $v){
                M('ptyh')->where(['uid' => $v])->setInc('tuiguang_num');
            }
        }
    }

    /**
     * 获取用户后代数量
     * @param string $path 路径
     * @param int $type 类型：0=全部后代，1=直属后代
     */
    public static function getChildCount($path,$type){
//        $totalsql = "SELECT count(*) count FROM jd_ptyh AS p WHERE p.path LIKE '{$path}' || '%' ";
        $totalsql = "SELECT count(*) count FROM jd_ptyh AS p WHERE p.path LIKE '{$path}%' ";
        $totalCount = M('ptyh')->query($totalsql); //包含自身和后代

        $count = $totalCount ? $totalCount[0]['count'] - 1 : 0;
        return $count;

    }


    //团队人数达到减点位
    public static function jiandianwei($path){
        $fenrunzhidu = \Home\Model\PtdataModel::getFenRunZhiDu();
        $fenrunzhiduKeys = array_keys($fenrunzhidu);


        // up_set2   减免值 $fenrunzhidu[$fenrunzhiduKeys[0]])
        //up_set 减免值  $fenrunzhidu[$fenrunzhiduKeys[0]]*100

//        $totalCount = 300;

        //64/90/97

        $pathArr = explode('/',$path);
        array_pop($pathArr);

        foreach($pathArr as $k => $v){
            $path = M('ptyh')->find($v);
            $gatherself = M('gather')->where(['uid' => $v])->find();
            $totalCount = \Home\Logic\UserLogic::getChildCount($path,0);

            if($totalCount - $fenrunzhiduKeys[0] < 0){

                $up_set2_Rate = $gatherself['up_set2'];
                $up_set_Rate = $gatherself['up_set'];
            }elseif($totalCount - $fenrunzhiduKeys[0] >= 0 && $totalCount - $fenrunzhiduKeys[1] < 0 ){
                $up_set2_Rate = ($gatherself['up_set2']*100 - $fenrunzhidu[$fenrunzhiduKeys[0]]*100)/100;
                $up_set_Rate = ($gatherself['up_set']*100 - $fenrunzhidu[$fenrunzhiduKeys[0]]*100)/100;

            }else{
                $up_set2_Rate = 0;
                $up_set_Rate = 0;
            }

            M('gather')->where(['uid' => $v])->save(['up_set' => $up_set_Rate, 'up_set2' => $up_set2_Rate]);

        }

    }
}